package org.example.curriculum_design_6_16.controller;

import org.example.curriculum_design_6_16.entity.vo.Result;
import org.example.curriculum_design_6_16.entity.pojo.ResearchPaper;
import org.example.curriculum_design_6_16.entity.vo.ResearchPaperVo;
import org.example.curriculum_design_6_16.service.ResearchPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/researchPaper")
public class ResearchPaperController {
    @Autowired
    private ResearchPaperService researchPaperService;

    @PostMapping
    public Result<Integer> addResearchPaper(@RequestBody ResearchPaper paper) {
        try {
            int result = researchPaperService.addResearchPaper(paper);
            if (result > 0) {
                return Result.success(result);
            } else {
                return Result.error("插入失败");
            }
        } catch (Exception e) {
            return Result.error("添加论文记录失败: " + e.getMessage());
        }
    }

    @PutMapping
    public Result<Integer> updateResearchPaper(@RequestBody ResearchPaper paper) {
        try {
            return Result.success(researchPaperService.updateResearchPaper(paper));
        } catch (Exception e) {
            return Result.error("更新论文记录失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result<Integer> deleteResearchPaper(@PathVariable Integer id) {
        try {
            return Result.success(researchPaperService.deleteResearchPaper(id));
        } catch (Exception e) {
            return Result.error("删除论文记录失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public Result<ResearchPaperVo> getResearchPaperById(@PathVariable Integer id) {
        try {
            return Result.success(researchPaperService.getResearchPaperById(id));
        } catch (Exception e) {
            return Result.error("获取论文记录失败: " + e.getMessage());
        }
    }

    @GetMapping("/student/{studentId}")
    public Result<List<ResearchPaperVo>> getResearchPapersByStudentId(@PathVariable String studentId) {
        try {
            return Result.success(researchPaperService.getResearchPapersByStudentId(studentId));
        } catch (Exception e) {
            return Result.error("获取学生论文记录失败: " + e.getMessage());
        }
    }

    @GetMapping
    public Result<List<ResearchPaperVo>> getAllResearchPapers() {
        try {
            return Result.success(researchPaperService.getAllResearchPapers());
        } catch (Exception e) {
            return Result.error("获取所有论文记录失败: " + e.getMessage());
        }
    }

    @PostMapping("/approve/{id}")
    public Result<Integer> approveResearchPaper(@PathVariable Integer id, @RequestParam String comment) {
        try {
            return Result.success(researchPaperService.approveResearchPaper(id, comment));
        } catch (Exception e) {
            return Result.error("审核论文失败: " + e.getMessage());
        }
    }

    @PostMapping("/reject/{id}")
    public Result<Integer> rejectResearchPaper(@PathVariable Integer id, @RequestParam String comment) {
        try {
            return Result.success(researchPaperService.rejectResearchPaper(id, comment));
        } catch (Exception e) {
            return Result.error("拒绝论文失败: " + e.getMessage());
        }
    }

    @GetMapping("/totalScore/{studentId}")
    public Result<Double> getTotalPaperScore(@PathVariable String studentId) {
        try {
            return Result.success(researchPaperService.getTotalPaperScore(studentId));
        } catch (Exception e) {
            return Result.error("计算论文总分失败: " + e.getMessage());
        }
    }
}